package com.wchen.gdoc.generate.producer;

import com.wchen.gdoc.generate.config.ApplicationConfigProvide;
import com.wchen.gdoc.generate.util.StringUtil;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * 测试参数数据生成
 *
 * @author  wchen
 * @date    2019/11/11
 */
public class DefaultParamValueProducer implements ParamValueProducer {
    private static char[] stringSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    private static int[] numberSequence = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    private static Random random = new Random();

    /**
     * 创建测试参数值
     *
     * @param classType
     * @param paramName
     * @return
     */
    @Override
    public Object createParamValue(String classType, String paramName) {
        Map<String, String> customData = ApplicationConfigProvide.getApplicationConfig().getCustomData();
        //返回值是没有paramName的
        if(StringUtil.isNotBlank(paramName)) {
            //关键词模糊匹配处理
            Set<String> customKeySet = customData.keySet();
            String customKey = customKeySet.stream().filter(s -> paramName.contains(s)).findFirst().orElseGet(() -> "");
            if (!StringUtils.isEmpty(customKey)) {
                return customData.get(customKey);
            }
        }

        //未匹配到、指定类型处理
        if (Long.class.getTypeName().equals(classType) || "long".equals(classType)) {
            return random.nextInt(1000);
        } else if (String.class.getTypeName().equals(classType)) {
            return getStringValue();
        } else if (Double.class.getTypeName().equals(classType) || "double".equals(classType)) {
            return random.nextInt(1000);
        } else if (BigDecimal.class.getTypeName().equals(classType)) {
            return random.nextInt(1000);
        } else if (Float.class.getTypeName().equals(classType) || "float".equals(classType)) {
            return random.nextInt(1000);
        } else if (Short.class.getTypeName().equals(classType) || "short".equals(classType)) {
            return random.nextInt(1000);
        } else if (Integer.class.getTypeName().equals(classType) || "int".equals(classType)) {
            return random.nextInt(1000);
        } else if (Boolean.class.getTypeName().equals(classType) || "boolean".equals(classType)) {
            return random.nextBoolean();
        } else if (Byte.class.getTypeName().equals(classType) || "byte".equals(classType)) {
            return Byte.valueOf(String.valueOf(stringSequence[random.nextInt(36)]));
        }else if (LocalDateTime.class.getTypeName().equals(classType)) {
            LocalDateTime localDateTime = LocalDateTime.now();
            return localDateTime.format(DateTimeFormatter.ofPattern("y-MM-d hh:mm:ss"));
        }else if (LocalDate.class.getTypeName().equals(classType)) {
            LocalDate localDate = LocalDate.now();
            return localDate.format(DateTimeFormatter.ISO_DATE);
        }else if (LocalTime.class.getTypeName().equals(classType)) {
            LocalDateTime localDateTime = LocalDateTime.now();
            return localDateTime.format(DateTimeFormatter.ofPattern("y-MM-d hh:mm:ss"));
        }else if (Date.class.getTypeName().equals(classType)) {
            LocalDateTime localDateTime = LocalDateTime.now();
            return localDateTime.format(DateTimeFormatter.ofPattern("y-MM-d hh:mm:ss"));
        } else {
            throw new RuntimeException(String.format("未支持的类型, classType:%s, paramsName:%s", classType, paramName));
        }
    }


    /**
     * 获取String 值
     * @return
     */
    public String getStringValue() {
        Integer length = random.nextInt(5) + 1;
        StringBuilder randomString = new StringBuilder();
        for(int i = 0; i< length; i++){
            randomString.append(stringSequence[random.nextInt(36)]);
        }
        return randomString.toString();
    }
}
